// 业务城市加载
window.onload = function(){
    // 加载赋值
    for (var i = 0; i < 25; i++) {
        console.log(String.fromCharCode((65 + i)));
         //先创建好select里面的option元素
          var option = document.createElement("option");
          //转换DOM对象为JQ对象,好用JQ里面提供的方法 给option的value赋值
          $(option).val(String.fromCharCode((65 + i)));
          //给option的text赋值,这就是你点开下拉框能够看到的东西
          $(option).text(String.fromCharCode((65 + i)));
          //获取select 下拉框对象,并将option添加进select
          $('#editSelectOption').append(option);
          
    }
    var url = 'http://192.168.0.102:8080/BusinessCity/getAllCityInfo'
    function s(datas) {
        function operateFormatter(value, row, index) {//赋予的参数
            return [
                '<button class="btn btn-primary" style="margin-right:10px"  onclick="editbusinessCity(' + row.cityid + ')">编辑</button>',
                '<button class="btn btn-primary" style="margin-right:10px"  onclick="busniessCity(' + row.cityid + ')">城区</button>'
            ].join('');
        }
         // 设置table列样式
         function  formatFormatter(value, row, index) {
            if (value == 1){
            
            return [
               
                '<img src="../../../images/success.png" onclick="statusChange(' + row.validflag+','+row.cityid + ')">',
            ].join('');
        }else{
            return [
               
                '<img src="../../../images/fail.png" onclick="statusChange(' + row.validflag +','+row.cityid + ')">',
            ].join('');
        }
        }
        var tableColumns = [
            {
                field: 'cityid',
                title: '城市ID',
                switchable: true
            }, {
                field: 'name',
                title: '名称',
                switchable: true
            },{
                field: 'prefix',
                title: '拼音首字母',
                switchable: true
            },
            {
                field: 'validflag',
                title: '状态',
                formatter: formatFormatter
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }
        ];
        $("#businessCity").bootstrapTable('destroy'); //分页必须写
        $("#businessCity").bootstrapTable({
            cache: false,
            pagination: true,
            sidePagination: "client",  //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,   //初始化加载第一页，默认第一页
            pageSize: 5,   //每页的记录行数（*）
            pageList: [5, 10], //可供选择的每页的行数
            columns: tableColumns,
            data: datas,
            queryParamsType: '',
            //得到查询的参数
            queryParams: function queryParams(params) {
                return params;
            },
            onLoadSuccess: function (data) {  //加载成功时执行
                alert("成功" + data)
            },
            onLoadError: function () {  //加载失败时执行
                alert("失败")
            }

        });
    }
    $.ajax({
        url: url,
        method : 'post',
        contentType: "application/x-www-form-urlencoded",
        // data:{ seriesID:xilieid},
        success: function (res) {
            // window.location.href='series.html'
            // console.log(res.data[0].brandid)
            console.log(res.data)
            // var dates = JSON.parse(data.data)
            s(res.data)
           
        }
    })
}
// 点击传值到城区界面
function busniessCity(cityid) {
    console.log(cityid)
     //跳转之前将局部页面保存到sessionStorage对象中。(键值名，值)
	//window.sessionStorage.setItem("backId",brandid);
    // 传递参数
    window.location.href="city.html?"+"txt="+cityid
}
// 城区编辑按钮
function editbusinessCity(cityid) {
    // 编辑
    divset = document.getElementsByClassName("search");
    console.log('name'+cityid)
    //var options=document.getElementById("editSelectOption").options;
    // 编辑赋值给编辑列表
    $.ajax({
         url: 'http://192.168.0.102:8080/BusinessCity/getEditCityInfo',
         method: 'post',//请求方式
         contentType: "application/x-www-form-urlencoded",
         data:{
            cityID:cityid
         },
         success: function (data) {
             console.log(data)
             console.log(data.data.prefix)
            // 编辑的名字
             $("#editCityname").val(data.data.name);
            //  编辑的首字母
            //options.value(data.data.prefix)
            $("#editSelectOption").find("option[value='"+data.data.prefix+"']").attr("selected",true);
            //隐藏cityID值
            $("#cityID").val(data.data.cityid)
         }
    })
    for (var i = 0; i<divset.length;i++) {
         divset[i].style.display="none";
    };
    divsetedit = document.getElementsByClassName("edit");
    for (var i = 0; i<divset.length;i++) {
     divsetedit[i].style.display="inline";
    };
 }
 // 编辑城市保存
function editsaveCity() {
    // 获取值
    var editcityName = document.getElementById("editCityname").value
    var editSelectOption = document.getElementById("editSelectOption").value
    var cityid = document.getElementById("cityID").value
    console.log("name" + editcityName + "abab" + editSelectOption)
    // 后台请求
    $.ajax({
        url: 'http://192.168.0.102:8080/BusinessCity/updateCity',//请求后台的url
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",
        data: {
            cityID: cityid,
            cityName: editcityName,
            prefix: editSelectOption
        },
        success: function (res) {
            console.log(res.msg)
            divset = document.getElementsByClassName("search");
      for (var i = 0; i<divset.length;i++) {
           divset[i].style.display="inline";
           location.reload();
      };
      divsetedit = document.getElementsByClassName("edit");
      for (var i = 0; i<divset.length;i++) {
          divsetedit[i].style.display="none";
      };
      divsetadd = document.getElementsByClassName("editAdd");
      for (var i = 0; i<divset.length;i++) {
          divsetadd[i].style.display="none";
      };  
            location.reload();
        }
    })
}
 //新增城市按钮
 function addcity(e) {
    for (var i = 0; i < 25; i++) {
        console.log(String.fromCharCode((65 + i)));
         //先创建好select里面的option元素
          var option = document.createElement("option");
          //转换DOM对象为JQ对象,好用JQ里面提供的方法 给option的value赋值
          $(option).val(String.fromCharCode((65 + i)));
          //给option的text赋值,这就是你点开下拉框能够看到的东西
          $(option).text(String.fromCharCode((65 + i)));
          //获取select 下拉框对象,并将option添加进select
          $('#selectOption').append(option);
    }
    // 添加
    divset = document.getElementsByClassName("search");
    for (var i = 0; i<divset.length;i++) {
         divset[i].style.display="none";
    };
    divsetadd = document.getElementsByClassName("editAdd");
    for (var i = 0; i<divset.length;i++) {
       divsetadd[i].style.display="inline";
    };
}
// 添加保存业务城市
function addsaveCity(){
    //获取值
     var businesscityName = document.getElementById("cityName").value
     var cityinitial = $('#selectOption option:selected').val();//选中的值
     $.ajax({
         url:'http://192.168.0.102:8080/BusinessCity/addCity',
         method:'post',
         contentType: "application/x-www-form-urlencoded",
         data: {
            cityName:businesscityName,
            prefix:cityinitial,
         },
         success: function (res) {
             console.log(res.data)
                // 返回
      divset = document.getElementsByClassName("search");
      for (var i = 0; i<divset.length;i++) {
           divset[i].style.display="inline";
           location.reload();
      };
      divsetedit = document.getElementsByClassName("edit");
      for (var i = 0; i<divset.length;i++) {
          divsetedit[i].style.display="none";
      };
      divsetadd = document.getElementsByClassName("editAdd");
      for (var i = 0; i<divset.length;i++) {
          divsetadd[i].style.display="none";
      };  
         } 
     })
   
}
function backcity(e) {
    // 返回
    divset = document.getElementsByClassName("search");
    for (var i = 0; i<divset.length;i++) {
         divset[i].style.display="inline";
    };
    divsetedit = document.getElementsByClassName("edit");
    for (var i = 0; i<divset.length;i++) {
        divsetedit[i].style.display="none";
    };
    divsetadd = document.getElementsByClassName("editAdd");
    for (var i = 0; i<divset.length;i++) {
        divsetadd[i].style.display="none";
    };
}
// 状态变化
// 城市状态
function statusChange(validflag,cityid) {
    // 获取当前状态
    console.log(validflag);
    // 判断当前状态更改
    if(validflag === 1){
       $('#modal-input').html('确认停用');
       $('#myModal').modal('show');
        var citystatus=0
        console.log("dianji"+citystatus+cityid);
        changeStata(citystatus,cityid)
    }
    else{
       $('#modal-input').html('确认启用');
       $('#myModal').modal('show');
        var citystatus=1
        console.log("dianji"+citystatus+cityid);
        changeStata(citystatus,cityid)
    }
}
// 城市状态确认更改按钮
// 切换状态
function changeStata(citystatus,cityid) {
    $('#btn_ok').click(function(){
    $.ajax({
        url: 'http://192.168.0.102:8080/BusinessCity/updateCityStatus',
        method:'post',
        contentType:'application/x-www-form-urlencoded',
        data:{
            cityID:cityid,
            validFlag:citystatus
        },
         success: function (res) {
            console.log(res.msg);
            $('#myModal').modal('hide');
            location.reload();
         }
    })
})
}
